using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Claims;
using ClaimsScaffold.Claims;

namespace ClaimsAugScaffold.Features.ClaimsAugScaffoldClaimProvider
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("fdf42a14-1350-443c-8694-62ef9e7dfe81")]
    public class ClaimsAugScaffoldClaimProviderEventReceiver : SPClaimProviderFeatureReceiver
    {
        /// <summary>
        /// The class constructor.
        /// </summary>
        public ClaimsAugScaffoldClaimProviderEventReceiver()
            : base()
        {
        }

        /// <summary>
        /// The full name of the claims assembly.
        /// </summary>
        public override string ClaimProviderAssembly
        {
            get
            {
                return typeof(ClaimProvider).Assembly.FullName;
            }
        }

        /// <summary>
        /// The claims provider description.
        /// </summary>
        public override string ClaimProviderDescription
        {
            get
            {
                return ClaimProvider.PROVIDER_DESCRIPTION;
            }
        }

        /// <summary>
        /// The name of the claims provider.
        /// </summary>
        public override string ClaimProviderDisplayName
        {
            get
            {
                return ClaimProvider.PROVIDER_NAME;
            }
        }

        /// <summary>
        /// The full type name of the claims provider.
        /// </summary>
        public override string ClaimProviderType
        {
            get
            {
                return typeof(ClaimProvider).FullName;
            }
        }

        /// <summary>
        /// Indicates the claim provider is not used by default.
        /// </summary>
        public override bool ClaimProviderUsedByDefault
        {
            get
            {
                return false;
            }
        }

        private void ModifyADClaimProvider(SPClaimProviderManager mgr, bool isEnabled)
        {

            IEnumerable<SPClaimProviderDefinition> providers = mgr.ClaimProviders;

            string action = isEnabled ? "Disabling" : "Enabling";

            foreach (SPClaimProviderDefinition defn in providers)
            {
                if (defn.ClaimProviderType.Equals(typeof(SPActiveDirectoryClaimProvider)))
                {
                    try
                    {

                        //LoggingService.LogMessage(TraceSeverity.High, "{0} the active directory claim provider", action);
                        defn.IsEnabled = isEnabled;
                        mgr.Update();
                        break;
                    }catch(Exception ex)
                    {
                        //LoggingService.LogError(ex, "Cannot '{0}' the active directory claim provider", action);
                    }
                }
            }

        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            base.FeatureActivated(properties);

            SPClaimProviderManager mgr = SPClaimProviderManager.Local;

            this.ModifyADClaimProvider(mgr, false);

            SPClaimProviderDefinition claimsScaffoldDefn = mgr.GetClaimProvider(ClaimProvider.PROVIDER_NAME);

            if( null != claimsScaffoldDefn)
            {
                claimsScaffoldDefn.IsUsedByDefault = true;
                mgr.Update();
            }
            else
            {
                //LoggingService.LogMessage(TraceSeverity.Unexpected, "'{0}' is not available!", ClaimProvider.PROVIDER_NAME);
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            base.FeatureDeactivating(properties);
            this.ModifyADClaimProvider(SPClaimProviderManager.Local, true);
        }


    }
}
